<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Hyr extends BaseController{

	protected $middleware = ['app\index\middleware\CheckHyrLogin::class'];
	public function initialize(){
		$system=Db::table('system')->find();
		view::assign('system',$system);
	}

	public function school(){
		$where['aid']=array('neq','');
		if(input('sname')!=''){
			$where['name']=array('like','%'.input('sname').'%');
		}
		if(session('utype')==1)
			$where['aid']=session('uid');
		$school=Db::table('school')->where($where)->orderRaw('convert(name using gbk)')->paginate(40);//halt($scho)
		$list=[];
		foreach($school as $a=>$b){
			$list[$a]=$school[$a];
			$res=Db::table('school_sign')->where('kg_id='.session('uid').' and sid='.$b['id'])->find();
			if($res){
				$list[$a]['sign']=1;
			}else{
				$list[$a]['sign']=0;
			}
		}
		return view('',['school'=>$school,'list'=>$list,'sname'=>input('sname')]);
	}

	//学员列表
	public function pingfen(){
		if(request()->isPost()){
			$a['aid']=session('uid');
			$a['state']=1;
			if (input('teacher')=='') {
				return json(['code'=>400,'msg'=>'请考官确认签字！']);
			}
			$a['teacher']=input('teacher');
			$data=input();
			$no='';
			for($i=0;$i<(count($data)-1)/2;$i++){
				$a['id']=$data['id'.$i];
				$a['score']=$data['score'.$i];
				if ($a['id']!='' && $a['score']!='') {
					$info=Db::table('student_card')->where('id',$a['id'])->find();
					if($info){
						$res=Db::table('student_card')->update($a);
						if($res===false){
							if($no==''){$no=$a['id'];}else{$no=','.$a;}
						}
					}else{
						if($no==''){$no=$a['id'];}else{$no=','.$a;}
					}
				}
			}
			if ($no=='') {
				return json(['code'=>200,'msg'=>'打分成功']);
			}else{
				return json(['code'=>400,'msg'=>$no.'号打分失败']);
			}
		}else{
			$where='(sc.sure_time="" or sc.sure_time is null)';
			$data=array();$pici=array();$card=array();$group=array();
			if (input('sid')!='') {
				$pici=Db::table('pici')->where('aid='.input('sid'))->order('id desc')->find();
				if($pici and $pici['id']!=''){
					$where.=' and s.sid='.input('sid').' and sc.pid='.$pici['id'];
					$card=Db::table('student_card')
						->alias('sc')
						->join('card c','sc.cid=c.id','left')
						->join('student s','sc.sid=s.id','left')
						->where($where)
						->field('c.id,c.name')
						->group('sc.cid')
						->order('c.id asc')
						->select();
				}
				$w1=[];
				if(session('utype')==1)
					$w1['id']=session('uid');
				$info=Db::table('school')->where('aid',input('sid'))->where($w1)->field('name')->find();
				if(!$info){
					die('无权访问');
				}
				if (input('cid')!='') {
					$where.=' and sc.cid='.input('cid');
					$group=Db::table('student_card')
						->alias('sc')
						->leftjoin('card c','sc.cid=c.id')
						->leftjoin('student s','sc.sid=s.id')
						->where($where)
						->field('sc.groups')
						->group('sc.groups')
						->order('sc.groups asc')
						->select()->toArray();
					if ($group) {
						foreach ($group as $k => $v) {
							$num[]=$v['groups'];
						}
						array_multisort($num,SORT_ASC,$group);//dump($group);die;
					}
				}
				if (input('sid')=='' || input('cid')=='' || input('gid')=='') {
					$list=array();
				}else{
					$where.=' and sc.score="" and sc.groups="'.input('gid').'"';
					$list=Db::table('student_card')
						->alias('sc')
						->join('card c','sc.cid=c.id','left')
						->join('student s','sc.sid=s.id','left')
						->where($where)
						->field('s.name,s.sex,s.age,s.img,c.name as cname,sc.id,sc.cid,sc.score,sc.groups,sc.num,sc.step')
						//->fetchsql()
						->order('sc.num+0')
						->select();//dump($list);dump(input('gid'));dump($group);die;
				}
				$kaoguan=Db::table('kaoguan')->select();
				return view('',['sname'=>$info['name'],'card'=>$card,'aid'=>input('sid'),'gid'=>input('gid'),'cid'=>input('cid'),'pid'=>$pici['id'],'group'=>$group,'list'=>$list,'kaoguan'=>$kaoguan,'pici'=>$pici]);
			}else{
				return redirect('/index/school.html');
			}
		}
	}

	function tongji(){
		if (session('utype')==2) {
			$this->redirect('/index/student.html');
		}
		$list=Db::table('student_card')
			->alias('sc')
			->join('card c','sc.cid=c.id','left')
			->where('sc.aid',session('uid'))
			->group('sc.cid')
			->order('c.id asc')
			->field('sc.aid,sc.cid,c.name')
			->select();
		if ($list) {
			foreach ($list as $k => $v) {
				$list[$k]['count']=Db::table('student_card')->where('aid='.$v['aid'].' and cid='.$v['cid'])->count();
			}
		}
		$count=Db::table('student_card')->where('aid',session('uid'))->count();
		return view('',['list'=>$list,'count'=>$count]);
	}

	//机构签名
	function jigou_sign(){
		if (request()->isPost()) {
			if (input('post.school')!='' && input('post.id')!='') {
				$school=input('post.school');
				$time=date('Ymd');
				$list=Db::table('student_card')
					->alias('sc')
					->join('student as s','sc.sid=s.id','inner')
					->field('sc.id')
					->where('s.sid='.input('post.id'))
					->select();
				if ($list) {
					$ids='';
					foreach ($list as $k => $v) {
						if ($ids=='') {
							$ids=$v['id'];
						}else{
							$ids.=','.$v['id'];
						}
					}
					$res=Db::table('student_card')->where('id in ('.$ids.')')->update(['school'=>$school,'sure_time'=>$time]);
					if ($res) {
						return json(['code'=>200,'msg'=>'操作成功']);
					}else{
						return json(['code'=>400,'msg'=>'考官提交签名出错']);
					}
				}else{
					return json(['code'=>400,'msg'=>'考官提交签名出错！']);
				}
			}else{
				return json(['code'=>400,'msg'=>'参数错误']);
			}
		}else{
			if (input('get.id')!='') {
				$list=Db::table('student_card')
					->alias('sc')
					->join('card c','sc.cid=c.id','inner')
					->join('kaoguan k','k.id=sc.teacher','left')
					->join('student s','sc.sid=s.id','inner')
					->where('s.sid='.input('id').' and (sc.sure_time="" or sc.sure_time is null)')
					->field('s.name,s.sex,s.age,s.img,c.name as cname,sc.id,sc.cid,sc.score,sc.groups,sc.num,sc.step,sc.state,sc.teacher as tid,k.name as tname')
					//->order('sc.groups asc,sc.num+0 asc')//->fetchsql()
					->select();//dump($list);//die;
				$jibie=array();//统计级别人数
				$qk=0;$bjg=0;//缺考人数
				$kgdf=array();//统计考官打分人数
				if ($list) {
					foreach ($list as $k => $v) {
						if ($v['score']=='缺考') {$qk++;}
						if ($v['score']=='不及格') {$bjg++;}
						if(!array_key_exists($v['cid'], $jibie)){
							$jibie[$v['cid']]['id']=$v['cid'];
							$jibie[$v['cid']]['name']=$list[$k]['cname'];
							$jibie[$v['cid']]['num']=1;
						}else{
							$jibie[$v['cid']]['num']++;
						}
						if ($v['tid']!='' && $list[$k]['tname']) {
							if(!array_key_exists($v['tid'], $kgdf)){
								$kgdf[$v['tid']]['id']=$v['tid'];
								$kgdf[$v['tid']]['name']=$list[$k]['tname'];
								$kgdf[$v['tid']]['num']=1;
							}else{
								$kgdf[$v['tid']]['num']++;
							}
						}
					}
				}
				//dump($qk);dump($jibie);dump($kgdf);
				$jigou=Db::table('school')->where('aid',input('id'))->find();
				return view('',['list'=>$list,'jibie'=>$jibie,'qk'=>$qk,'kgdf'=>$kgdf,'jigou'=>$jigou,'bjg'=>$bjg]);
			}else{
				die('参数错误');
			}
		}
	}
}